
# ------------------------------------------------------------------------------------------
# # Julia中的基本线性代数
# Author: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/)
# (with edits from Jane Herriman)
# ------------------------------------------------------------------------------------------

# ------------------------------------------------------------------------------------------
# 首先定义一个随机矩阵
# ------------------------------------------------------------------------------------------

A = rand(1:4,3,3)

# ------------------------------------------------------------------------------------------
# 定义一个元素全为1的向量
# ------------------------------------------------------------------------------------------

x = fill(1.0, (3,)) # = fill(1.0, 3)

# ------------------------------------------------------------------------------------------
# 注意$A$的类型为Array{Int64,2}，而$x$的类型为Array{Float64,1}。Julia定义向量Vector{Type}的别名为Array{Type,1}，矩阵
# Matrix{Type}的别名为Array{Type,2}。
#
# 许多基础操作和其他语言一样
# #### 乘法
# ------------------------------------------------------------------------------------------

b = A*x

# ------------------------------------------------------------------------------------------
# #### 转置
# 就像在其他语言中`A'`表示对`A`的共轭转置或者伴随矩阵
# ------------------------------------------------------------------------------------------

A'

# ------------------------------------------------------------------------------------------
# 我们可以通过transpose获得转置矩阵
# ------------------------------------------------------------------------------------------

transpose(A)

# ------------------------------------------------------------------------------------------
# #### 转置的乘法
# Julia中某些情况下可以省略`*`号
# ------------------------------------------------------------------------------------------

A'A

# ------------------------------------------------------------------------------------------
# #### 解线性方程组
# 用方阵$A$表示的线性方程组$Ax=b$用左除运算符（函数）`\`求解
# ------------------------------------------------------------------------------------------

A\b

# ------------------------------------------------------------------------------------------
# 超定线性方程组（overdetermined linear system）的情况下`A\b`给出*最小二乘解（least squares solution）*
# ------------------------------------------------------------------------------------------

Atall = rand(3, 2)

Atall\b

# ------------------------------------------------------------------------------------------
# 缺秩最小二乘问题（a rank-deficient least squares problem）情况下，会给出*极小范数最小二乘解（minimum norm least
# squares solution）*
# ------------------------------------------------------------------------------------------

v = rand(3)
rankdef = hcat(v, v)

rankdef\b

# ------------------------------------------------------------------------------------------
# 欠定问题（an underdetermined solution）下，会给出 最小范数解（minimum norm solution）
# ------------------------------------------------------------------------------------------

bshort = rand(2)
Ashort = rand(2, 3)

Ashort\bshort

# ------------------------------------------------------------------------------------------
# # LinearAlgebra包
#
# 如上所示，Julia默认自带了很多线性代数操作，而标准库`LinearAlgebra`提供了更多的相关名词和函数。特别是它提供了因子分解和一些结构化矩阵类型。和其他包一样，可以通过
# `using LinearAlgebra`引入这些额外的特征。
# ------------------------------------------------------------------------------------------

# ------------------------------------------------------------------------------------------
# ### 练习
#
# #### 10.1
# 计算向量`v`和它自己的内积，并赋值给`dot_v`。
# ------------------------------------------------------------------------------------------

v = [1,2,3]



@assert dot_v == 14

# ------------------------------------------------------------------------------------------
# #### 10.2
# 计算向量v和它自己的外积，并赋值给`outer_v`。
# ------------------------------------------------------------------------------------------



@assert outer_v == [1 2 3
                    2 4 6
                    3 6 9]

@assert cross_v == [0, 0, 0]

# ------------------------------------------------------------------------------------------
# 完成练习后请点击顶部的`Validate`按钮。
# ------------------------------------------------------------------------------------------
